<?php

class system extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);
		
		$this->extend('__admin_system_types');
		$this->extend('__admin_system_fields');
		$this->extend('__admin_system_guides');
		$this->extend('__admin_system_images_profiles');
		$this->extend('__admin_system_types_connections');
		$this->extend('__admin_system_update');
		$this->extend('__admin_system_modules');
		$this->extend('__admin_system_rss');
	}
	
	public function __default () {
		redirect (HTTP_HOST . '/admin');
	}
	
	public function regedit () {
		$this->setPageTitle (lang ('admin_header_regedit','system'));
		
		if (getRequest('create',' bool')) {
			$key = isset($_POST['new_key']) ? $_POST['new_key'] : '';
			$value = isset($_POST['new_value']) ? $_POST['new_value'] : '';
			$help = isset($_POST['new_help']) ? $_POST['new_help'] : '';
			
			if ($key) {			
				if ($help == '') {
					$help = false;
				}
				if ($value == '') {
					$value = false;
				}
				
				Regedit::getInstance()->set ($key, $value, $help);
				redirect (HTTP_HOST . '/admin/system/regedit?ok');
			}
		}
		
		$key = isset($_POST['key']) ? $_POST['key'] : false;
		
		if ($key && isset($_POST['value'])) {
			Regedit::getInstance()->set ($key, $_POST['value']);
			redirect (HTTP_HOST . '/admin/system/regedit?ok');
		}
		
		$response = new ModuleResponse;
		$response->setTemplate ('system/regedit');
		$response->setAttribute ('key', $key);
		
		if (getRequest ('ok', 'bool')) {
			$response->addObject (new SystemMessage (lang('regedit_saved','system')));
		}
		
		if ($key) {
			$value = Regedit::getInstance()->get ($key);
			$response->setAttribute('edit', 1);
		} else {
			$value = '';
			$response->setAttribute('edit', 0);
		}
		
		$response->addObject ($value);
		$response->addObject (new Array2XML ('helpers', Regedit::getInstance()->getHelpers()));		
		
		return $response;
	}

	public function edit_uris () {
		if (getRequest('submit','bool') && isset($_POST['uri']) && is_array($_POST['uri'])) {
			$uris = $_POST['uri'];
			
			$str = "<?php\r\n\r\n\$REWRITE = array (";
			$temp = array();
			
			foreach ($uris as $uri) {
				if (!is_array($uri) || !isset($uri['from']) || trim($uri['from']) == '' || !isset($uri['to']) || trim($uri['to']) == '') {
					continue;
				}
				
				$temp[] = "\r\n\t'" . trim($uri['from']) . "' => '" . trim($uri['to']) . "'";
			}
			
			$str .= implode($temp, ',') . "\r\n);";
			
			$f = fopen(ENGINE_ROOT . '/URIs.php', 'w+');
			fwrite($f, $str);
			fclose($f);
			
			redirect(HTTP_HOST . '/admin/system/edit_uris');
		}
		
		include ENGINE_ROOT . '/URIs.php';
		
		$arr = array();
		
		foreach($REWRITE as $key => $uri) {
			$arr[]['uri'] = array ('from' => $key, 'to' => $uri);
		}
		
		$response = new ModuleResponse;
		$response->setTemplate('system/uris');
		$response->addObject(new Array2XML('uris', $arr));
		
		return $response;
	}
	
	public function test_converter () {
		$oConverter = new TypesConverter(array(TypesController::getInstance()->getTypeByName('banner')));
		$xml = $oConverter->getXMLData();
		$this->flush($xml->saveXML(), 'text/xml');
	}
	
	/*public function show_connections () {
		$typesController = TypesController::getInstance();
		$fieldsController = FieldsController::getInstance();
		$objectsController = ObjectsController::getInstance();
		
		$oTypeConnection = $typesController->getTypeByName ('explay_types_connection');
		
		$sel = new Selection;
		$sel->addType ($oTypeConnection);
		$sel->setOrder ('parent_type_id');
		
		$res = $sel->getList ();
		
		print "START TRANSACTION;<br>";
		
		foreach ($res as $row) {
			$object = $objectsController->getObject ($oTypeConnection, $row[0]);
			
			$oParentType = $typesController->getTypeById ($object->getValue ('parent_type_id'));
			$oChildType = $typesController->getTypeById ($object->getValue ('child_type_id'));
			$parentFieldName = $object->getValue ('parent_field_id') == 0 ? 'id' : $fieldsController->getField($object->getValue ('parent_field_id'))->getName();
			$childFieldName = $fieldsController->getField($object->getValue ('child_rel_field_id'))->getName();
			
			print "ALTER TABLE `" . $oChildType->getTableName() . "` ADD CONSTRAINT  FOREIGN KEY (`" . $childFieldName . "`) REFERENCES `" . $oParentType->getTableName() . "` (`" . $parentFieldName . "`) ON DELETE CASCADE ON UPDATE CASCADE;<br>";
		}
		
		print "COMMIT;";
		
		exit;
	}*/
}